<template>
	<view>
		<cu-custom bgColor="linear-gradient(to right, rgba(0,0,0,0.7), rgba(0,0,0,0.6))" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">我的计划</block>
		</cu-custom>
		<view class="cu-form-group">
			<view class="title">是否编辑</view>
			<switch class='switch-sex' @change="SwitchC" :class="switchC?'checked':''" :checked="switchC?true:false"></switch>
		</view>
		<form @submit="formSubmit" @reset="formReset">
			<view class="cu-form-group margin-top">
				<view class="title">计划标题</view>
				<input placeholder="请输入计划标题" v-model="planInfo.title" :disabled="!switchC"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">计划标签</view>
				<input placeholder="计划标签请以',' 分割" v-model="planInfo.tags" :disabled="!switchC"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">计划日期</view>
				<picker mode="date" start="2015-09-01" end="2200-09-01" value-format="yyyyMMdd" @change="DateChange">
					<view class="picker">
						{{planInfo.date}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group align-start">
				<view class="title">我想要做：</view>
				<textarea maxlength="-1" v-model="planInfo.content" placeholder="多行文本输入框"></textarea>
			</view>
			<view class="uni-btn-v" style="text-align: center;">
				<button class="cu-btn round bg-purple" form-type="submit" :disabled="!switchC">Submit</button>
				<button class="cu-btn round bg-purple" type="default" form-type="reset" :disabled="!switchC">Reset</button>
			</view>
		</form>
	</view>
</template>

<script>
	var _this;
	import {
		savePlan,
	} from '../../service/api/plan.js' //登录api
	export default {
		data() {
			return {
				planInfo:{
					date:"20190101",
					dateFormat:"2019-01-01",
				},
				switchC: false,
			};
		},
		onLoad(){
			const value = uni.getStorageSync('userinfo');
			console.log(value);
			this.planInfo.authorId = value.userId;
			_this = this
		},
		methods: {
			SwitchC(e) {
				this.switchC = e.detail.value
			},
			DateChange(e){
				console.log(e.detail.value)
				this.planInfo.date = e.detail.value.replace(/-/g,"");
				this.planInfo.dateFormat = e.detail.value
			},
			formSubmit: function(e) {
			                console.log('form发生了submit事件，携带数据为：' + JSON.stringify(this.planInfo))
			                var formdata = this.planInfo
			                savePlan(formdata)
			                .then(res => {
			                	console.log(res) 
			                	//简单验证下登录（不安全）
			                	if(res.data.code==0){
			                		uni.reLaunch({
			                			url: '../index?PageCur=plan',
			                		});
			                	}else{
			                	}
			                	uni.hideLoading();
			                }).catch(err => {
			                	console.log(err)
			                	let msg = err.errMsg; //错误信息
			                	uni.showToast({
			                	    icon: 'none',
			                		position: 'center',
			                	    title: '网络开小差了，小主稍后再试哦^.^'
			                	});
			                	uni.hideLoading();
							})
			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
